package gui.crud;

import gui.util.NumberOnlyListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import domain.PTime;

import persistence.DisciplineHandler;

public class DisciplineEditPanel extends AbstractEditPanel {

	private DisciplineHandler d;
	
	private JTextField dName, dCode;
	private JComboBox<String> professorBox;
	private JComboBox<Integer> periodField, hourField;
	private JLabel dNameLabel, dCodeLabel, dHourLabel, dPeriodLabel, dProfessorLabel;
	
	public DisciplineEditPanel() {
		super("Administrar Disciplinas", 530, 400);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void setUpNewComponents() {
		dName = new JTextField();
		dCode = new JTextField();
		professorBox = new JComboBox<String>();
		periodField = new JComboBox<Integer>();
		hourField = new JComboBox<Integer>();
		dNameLabel = new JLabel("<html><i><b>Nome:</b></i></html>");
		dCodeLabel = new JLabel("<html><i><b>Codigo:</b></i></html>");
		dHourLabel = new JLabel("<html><i><b>Duracao:</b></i></html>");
		dPeriodLabel = new JLabel("<html><i><b>Periodo:</b></i></html>");
		dProfessorLabel = new JLabel("<html><i><b>Professor(es):</b></i></html>");
		
		dCode.addKeyListener(new NumberOnlyListener());
		
		//como funciona? d.retreive("codigo");
		for (int i = 1; i < 9; i++) {
			periodField.addItem(i);
		}
		
		for (int i = 1; i < 7; i++) {
			hourField.addItem(i);
		}
		
		dName.setBounds(60, 130, 120, 20);
		dCode.setBounds(200, 130, 90, 20);
		professorBox.setBounds(200, 190, 120, 20);
		periodField.setBounds(60, 190, 50, 20);
		hourField.setBounds(131, 190, 50, 20);
		dNameLabel.setBounds(60, 108, 120, 20);
		dCodeLabel.setBounds(200, 108, 120, 20);
		dHourLabel.setBounds(131, 168, 80, 20);
		dPeriodLabel.setBounds(60, 168, 80, 20);
		dProfessorLabel.setBounds(200, 168, 120, 20);
	}

	@Override
	protected void addNewComponents() {
		this.add(dName);
		this.add(dCode);
		this.add(professorBox);
		this.add(periodField);
		this.add(hourField);
		this.add(dNameLabel);
		this.add(dCodeLabel);
		this.add(dHourLabel);
		this.add(dPeriodLabel);
		this.add(dProfessorLabel);
	}
	
	@Override
	protected void confirmChanges() {
		//Pegar os dados
		PTime p;
		try {
			p = new PTime((Integer)hourField.getSelectedItem(), 0);
			d.update(dCode.getText(), dName.getText(), (Integer)periodField.getSelectedItem(), null, null, p );
		} catch (Exception e) {}
		
	}

	@Override
	protected void deleteAction() {
		d.delete(dCode.getText());
	}

	

	@Override
	protected void setObjectsTypeAndContent() {
		getObjectsLabel().setText("Disciplinas");
	}

	@Override
	protected void updateFields() {
		// TODO Auto-generated method stub
		
	}

}
